package com.tedu._02notice.controller;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.tedu._02notice.base.response.JsonResult;
import com.tedu._02notice.base.response.StatusCode;
import com.tedu._02notice.mapper.NoticeMapper;
import com.tedu._02notice.pojo.dto.NoticeAddParam;
import com.tedu._02notice.pojo.dto.NoticeAddQuery;
import com.tedu._02notice.pojo.dto.NoticeUpdateParm;
import com.tedu._02notice.pojo.entity.Notice;
import com.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import com.tedu._02notice.pojo.vo.NoticeListVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
@Slf4j//lombok提供的日志注解
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private  NoticeMapper noticeMapper;

    @PostMapping("add")
    public JsonResult add(NoticeAddParam noticeAddParam){
//        System.out.println("添加公告："+noticeAddParam);
        log.debug("添加公告："+noticeAddParam);
        Notice  notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.inster(notice);

        return JsonResult.ok();
    }

    @GetMapping("list")
    public JsonResult list(NoticeAddQuery noticeAddQuery){
        log.debug("查询公告："+noticeAddQuery);
        String title= noticeAddQuery.getTitle();
        Integer type= noticeAddQuery.getType();
        List<NoticeListVO>noticeListVOS=noticeMapper.list(title,type);
        return JsonResult.ok(noticeListVOS);
    }
    @GetMapping("detail")
    public JsonResult detailInfoVO(Long id){
        NoticeDetailInfoVO detailInfoVO=noticeMapper.detail(id);
        return JsonResult.ok(detailInfoVO);
    }
    @PostMapping("delete")
    public JsonResult delete(Long id){
        int num=noticeMapper.delete(id);
        if (num>0){
            return JsonResult.ok();
        }else {
            return JsonResult.ok(StatusCode.OPERATION_FAILED);
        }
    }
    @PostMapping("update")
    public JsonResult date(NoticeUpdateParm noticeUpdateParm){
        log.debug("修改公告："+noticeUpdateParm);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeUpdateParm,notice);
        notice.setUpdateTime(new Date());
        int num=noticeMapper.update(notice);
        if (num>0){
            return JsonResult.ok();
        }else {
            return JsonResult.ok(StatusCode.OPERATION_FAILED);
        }
    }
}
